package com.zhoug.map3.dianditu;

import androidx.annotation.NonNull;

/**
 * 天地图瓦片资源创建工具
 *
 * @Author: zhoug
 * @Date: 2025-01-02
 * @Description:
 */
public class TianDiTuTileSourceFactory {
    //矢量底图-经纬度投影
    public static final String VEC_C = "vec_c";
    //矢量底图-球面墨卡托投影
    public static final String VEC_W = "vec_w";
    //矢量注记-经纬度投影
    public static final String CVA_C = "cva_c";
    //矢量注记-球面墨卡托投影
    public static final String CVA_W = "cva_w";
    //影像底图-经纬度投影
    public static final String IMG_C = "img_c";
    //影像底图-球面墨卡托投影
    public static final String IMG_W = "img_w";
    //影像注记-经纬度投影
    public static final String CIA_C = "cia_c";
    //影像注记-球面墨卡托投影
    public static final String CIA_W = "cia_w";
    //地形晕渲-经纬度投影
    public static final String TER_C = "ter_c";
    //地形晕渲-球面墨卡托投影
    public static final String TER_W = "ter_w";
    //地形注记-经纬度投影
    public static final String CTA_C = "cta_c";
    //地形注记-球面墨卡托投影
    public static final String CTA_W = "cta_w";
    //全球境界-经纬度投影
    public static final String IBO_C = "ibo_c";
    //全球境界-球面墨卡托投影
    public static final String IBO_W = "ibo_w";

    private static final String VEC = "vec";
    private static final String CVA = "cva";
    private static final String IMG = "img";
    private static final String CIA = "cia";
    private static final String TER = "ter";
    private static final String CTA = "cta";
    private static final String IBO = "ibo";

    private static final String PATH_WMTS = "/wmts";


    private final String layerName;
    private final String layer;
    private final String mapKey;
    private String service = "WMTS";
    private String request = "GetTile";
    private String version = "1.0.0";
    private String style = "default";
    private String tileMatrixSet = "w";
    private String format = "tiles";

    private String name = "TianDiTu";
    private int minZoomLevel = 1;
    private int maxZoomLevel = 18;
    private int tileSizePixels = 256;
    private String imageFileNameEnding = "";


    /**
     * 默认值{@link #VEC_W}
     */
    public TianDiTuTileSourceFactory(String mapKey) {
        this(VEC_W, mapKey);
    }

    /**
     * 默认值{@link #VEC_W}
     *
     * @param layerName
     */
    public TianDiTuTileSourceFactory(@NonNull String layerName, String mapKey) {
        this.layerName = layerName;
        this.mapKey = mapKey;
        layer = getLayer();
    }

    public void setService(String service) {
        this.service = service;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void setTileMatrixSet(String tileMatrixSet) {
        this.tileMatrixSet = tileMatrixSet;
    }

    public void setFormat(String format) {
        this.format = format;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setMinZoomLevel(int minZoomLevel) {
        this.minZoomLevel = minZoomLevel;
    }

    public void setMaxZoomLevel(int maxZoomLevel) {
        this.maxZoomLevel = maxZoomLevel;
    }

    public void setImageFileNameEnding(String imageFileNameEnding) {
        this.imageFileNameEnding = imageFileNameEnding;
    }

    public void setTileSizePixels(int tileSizePixels) {
        this.tileSizePixels = tileSizePixels;
    }

    /**
     * 获取所有域名集合
     *
     * @return
     */
    public String[] getBaseUrls() {
        String[] baseUrls = new String[8];
        for (int i = 0; i < 8; i++) {
            baseUrls[i] = getBaseUrl(i);
        }
        return baseUrls;
    }

    /**
     * 天地图地图服务二级域名包括t0-t7
     *
     * @param index
     * @return
     */
    private String getBaseUrl(int index) {
        return "http://t" + (index % 8) + ".tianditu.gov.cn/" + layerName + PATH_WMTS +
                "?SERVICE=" + service + "&REQUEST=" + request + "&VERSION=" + version
                + "&LAYER=" + layer + "&STYLE=" + style + "&TILEMATRIXSET=" + tileMatrixSet
                + "&FORMAT=" + format + "&tk=" + mapKey;
    }

    /**
     * 获取瓦片资源地址
     *
     * @param baseUrl
     * @param zoom
     * @param row
     * @param col
     * @return
     */
    public static String getTileURLString(String baseUrl, int zoom, int row, int col) {
        return baseUrl + "&TILEMATRIX=" + zoom + "&TILEROW=" + row + "&TILECOL=" + col;
    }


    private String getLayer() {
        return switch (layerName) {
            case VEC_C, VEC_W -> VEC;
            case CVA_C, CVA_W -> CVA;
//            case IMG_C, IMG_W -> IMG;
            case CIA_C, CIA_W -> CIA;
            case TER_C, TER_W -> TER;
            case CTA_C, CTA_W -> CTA;
            case IBO_C, IBO_W -> IBO;
            default -> IMG;
        };
    }

    public TianDiTuTileSource getTileSource() {
        return new TianDiTuTileSource(name, minZoomLevel, maxZoomLevel, tileSizePixels, imageFileNameEnding, getBaseUrls());
    }

}
